package com.me.mygdxgame;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.utils.Array;
import com.me.mygdxgame.models.AbstractGameObject;
import com.me.mygdxgame.models.GoldCoin;
import com.me.mygdxgame.models.Gun;
import com.me.mygdxgame.models.Item;
import com.me.mygdxgame.models.Player;
import com.me.mygdxgame.models.PotGold;
import com.me.mygdxgame.models.Stone;
import com.me.mygdxgame.models.Stone.TYPE;
import com.me.mygdxgame.util.Constants;

public class LevelTile {

	public static final String TAG = Level.class.getName();
	public Array<GoldCoin> goldCoins;
	public Array<Gun> guns;
	public Array<PotGold> potGolds;
	public Array<Item> items;
	public Array<Stone> stones;
	public TiledMap map;
	public Player player;
	
	public enum BLOCK_TYPE{
		
		ITEM_GOLD_COIN,
		ITEm_GUN,
		ITEm_POT_GOLD;
		
	}
		
	public LevelTile(String filename){
		init(filename);
	}
		
	private void init(String filename) {
		goldCoins = new Array<GoldCoin>();
		guns = new Array<Gun>();
		potGolds = new Array<PotGold>();
		items = new Array<Item>();
		stones = new Array<Stone>();
		map = new TmxMapLoader().load("map/Level1.tmx");
		
		MapLayer layer = map.getLayers().get("coin");
		
//		RectangleMapObject[] rects = new RectangleMapObject[layer.getObjects().getCount()];
		for (int i = 0; i < layer.getObjects().getCount(); i++) {
			MapObject obj = layer.getObjects().get(i);
			RectangleMapObject rect = (RectangleMapObject) obj;
			AbstractGameObject obj1 = null;
//			rects[i] = rect;
			obj1 = new GoldCoin();
			obj1.sprite.setPosition(rect.getRectangle().x, rect.getRectangle().y);
			obj1.sprite.setBounds(obj1.sprite.getX(), obj1.sprite.getY(), obj1.sprite.getWidth(), obj1.sprite.getHeight());
			goldCoins.add((GoldCoin)obj1);
		}
		
		MapLayer layerGun = map.getLayers().get("gun");
		
//		RectangleMapObject[] rectsGun = new RectangleMapObject[layerGun.getObjects().getCount()];
		for (int i = 0; i < layerGun.getObjects().getCount(); i++) {
			MapObject obj = layerGun.getObjects().get(i);
			RectangleMapObject rect = (RectangleMapObject) obj;
			AbstractGameObject obj1 = null;
//			rectsGun[i] = rect;
			obj1 = new Gun();
			obj1.sprite.setPosition(rect.getRectangle().x, rect.getRectangle().y);
			obj1.sprite.setBounds(obj1.sprite.getX(), obj1.sprite.getY(), obj1.sprite.getWidth(), obj1.sprite.getHeight());
			guns.add((Gun)obj1);
		}
		
		MapLayer layerPotGold = map.getLayers().get("potgold");
		
//		RectangleMapObject[] rectsPotGold = new RectangleMapObject[layerPotGold.getObjects().getCount()];
		for (int i = 0; i < layerPotGold.getObjects().getCount(); i++) {
			MapObject obj = layerPotGold.getObjects().get(i);
			RectangleMapObject rect = (RectangleMapObject) obj;
			AbstractGameObject obj1 = null;
//			rectsPotGold[i] = rect;
			obj1 = new PotGold();
			obj1.sprite.setPosition(rect.getRectangle().x, rect.getRectangle().y);
			obj1.sprite.setBounds(obj1.sprite.getX(), obj1.sprite.getY(), obj1.sprite.getWidth(), obj1.sprite.getHeight());
			potGolds.add((PotGold)obj1);
		}
		
		MapLayer layerItem = map.getLayers().get("stone");
		
//		RectangleMapObject[] rectsItem = new RectangleMapObject[layerItem.getObjects().getCount()];
		for (int i = layerItem.getObjects().getCount() - 1; i >= 0; i--) {
			MapObject obj = layerItem.getObjects().get(i);
			RectangleMapObject rect = (RectangleMapObject) obj;
			if(obj.getProperties().get("yellow") != null){
				AbstractGameObject obj1 = null;
//				rectsItem[i] = rect;
				obj1 = new Item();
				obj1.sprite.setPosition(rect.getRectangle().x, rect.getRectangle().y);
				obj1.sprite.setBounds(obj1.sprite.getX(), obj1.sprite.getY(), obj1.sprite.getWidth(), obj1.sprite.getHeight());
				items.add((Item)obj1);
			}else if(obj.getProperties().get("1") != null){
				AbstractGameObject obj1 = null;
//				rectsItem[i] = rect;
				obj1 = new Stone(TYPE.ONE);
				obj1.sprite.setPosition(rect.getRectangle().x, rect.getRectangle().y);
				obj1.sprite.setBounds(obj1.sprite.getX(), obj1.sprite.getY(), obj1.sprite.getWidth(), obj1.sprite.getHeight());
				stones.add((Stone)obj1);
			}else if(obj.getProperties().get("2") != null){
				AbstractGameObject obj1 = null;
//				rectsItem[i] = rect;
				obj1 = new Stone(TYPE.TWO);
				obj1.sprite.setPosition(rect.getRectangle().x, rect.getRectangle().y);
				obj1.sprite.setBounds(obj1.sprite.getX(), obj1.sprite.getY(), obj1.sprite.getWidth(), obj1.sprite.getHeight());
				stones.add((Stone)obj1);
			}else if(obj.getProperties().get("3") != null){
				AbstractGameObject obj1 = null;
//				rectsItem[i] = rect;
				obj1 = new Stone(TYPE.THREE);
				obj1.sprite.setPosition(rect.getRectangle().x, rect.getRectangle().y);
				obj1.sprite.setBounds(obj1.sprite.getX(), obj1.sprite.getY(), obj1.sprite.getWidth(), obj1.sprite.getHeight());
				stones.add((Stone)obj1);
			}
		}
		
//		MapLayer layerPlayer = map.getLayers().get("player");
//		
//		RectangleMapObject[] rectsPlayer = new RectangleMapObject[layerPotGold.getObjects().getCount()];
//		MapObject obj = layerPlayer.getObjects().get(0);
//		RectangleMapObject rect = (RectangleMapObject) obj;
//		AbstractGameObject obj1 = null;
//		rectsPlayer[0] = rect;
//		obj1 = new Player();
//		obj1.position.set(rect.getRectangle().x/40 , rect.getRectangle().y/40);
//		player =  (Player)obj1;
		
	}

//	public RectangleMapObject[] getRectangMapObject(String name_layer, TiledMap map) {
//		MapLayer layer = map.getLayers().get(name_layer);
//		if (layer == null)
//			return null;
//		RectangleMapObject[] rects = new RectangleMapObject[layer.getObjects().getCount()];
//		for (int i = 0; i < layer.getObjects().getCount(); i++) {
//			MapObject obj = layer.getObjects().get(i);
//			RectangleMapObject rect = (RectangleMapObject) obj;
//			rects[i] = rect;
//		}
//		return rects;
//	}

	public void render(SpriteBatch batch) {
		// TODO Auto-generated method stub
		
		for(Gun gun : guns){
			gun.render(batch);
		}
		for(PotGold potGun : potGolds){
			potGun.render(batch);
		}
		for(Item item : items){
			item.render(batch);
		}
		for(Stone stone : stones){
			stone.render(batch);
		}
		for(GoldCoin goldCoin : goldCoins){
			goldCoin.render(batch);
		}
//		player.render(batch);
		
	}
	
	public void update(float deltaTime){
		for(Gun gun : guns){
			gun.update(deltaTime);
		}
		for(PotGold potGold : potGolds){
			potGold.update(deltaTime);
		}
		for(Item item : items){
			item.update(deltaTime);
		}
		for(Stone stone : stones){
			stone.update(deltaTime);
		}
		for(GoldCoin goldCoin : goldCoins){
			goldCoin.update(deltaTime);
		}
	}
}
